﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class JoystickWindow : MonoBehaviour
{
    public bool showGUI_Joystick = true;
    public bool showGUI_XBox = false;
    public bool showGUI_X56 = false;

    public bool ShouldShowGUI = false;

    public Rect windowRect_Joystick = new Rect(15f, 100f, 356f, 180f);
    public Rect windowRect_Axis = new Rect(15f, 100f, 300f, 300f);
    public Rect windowRect_XBox = new Rect(15f, 100f, 356f, 288f/* + 100f + 75f*/);
    public Rect windowRect_X56 = new Rect(15f, 100f, 402f-20f, 348f);
    

    public static int width = 300;
    public static int height = 300;
    public Rect windowRect_test;
    

    public string WindowName_JoyStick = "Joystick Console Window Ctrl+F10";
    public string windowName_XBox  = "XBox Window";
    public string windowName_X56 = "X56 Window";
    public int windowID_Joystick = Modding.ModUtility.GetWindowId();
    public int windowID_Axis = Modding.ModUtility.GetWindowId();
    public int windowID_XBox = Modding.ModUtility.GetWindowId();
    public int windowID_X56 = Modding.ModUtility.GetWindowId();
    

    public int windowID_test = Modding.ModUtility.GetWindowId();

    public StickType stickType = StickType.XBox;


    public List<string> AxisValues = new List<string>();

    private Rect _graphRect;
    private Rect _lastGraphRect;
    private Texture2D _graphTex;
    private Color[] _resetTex;

    private JoyAxis _joyAxis;

    public enum StickType
    {
        XBox = 0,
        X56 = 1
    }

    public Texture2D testTex = new Texture2D(100,100);
    public void Awake()
    {
        //windowRect_XBox = new Rect(15f, 100f, 356f, 288f+100f + 75f);
        //windowName_XBox = "JoyStick Window  Ctrl+F10";

        windowRect_test = new Rect(15f, 100f, width, height);
        //testTex.SetPixel

        _joyAxis = JoyAxis.Default;
    }

    private void Update()
    {     
        if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.F10))
        {
            showGUI_Joystick = !showGUI_Joystick;
        }
        ShouldShowGUI = showGUI_Joystick /*&& !StatMaster.levelSimulating*/ && IsBuilding() && !StatMaster.inMenu;

       

    }

    delegate bool addToggle(string title, bool value);

    void OnGUI()
    {
        if (ShouldShowGUI)
        {
            windowRect_Joystick = GUILayout.Window(windowID_Joystick, windowRect_Joystick, Window_Console, WindowName_JoyStick);

            if (showGUI_XBox)
            {
                windowRect_XBox = GUILayout.Window(windowID_XBox, windowRect_XBox, new GUI.WindowFunction(Window_XBox), windowName_XBox);
            }

            if (showGUI_X56)
            {
                windowRect_X56 = GUILayout.Window(windowID_X56, windowRect_X56, new GUI.WindowFunction(Window_X56), windowName_X56);
            }

            //windowRect_test = GUILayout.Window(windowID_test, windowRect_test, Window_test, "test window");
            windowRect_Axis = GUILayout.Window(windowID_Axis, windowRect_Axis, Window_Axis, "Axis Mapper");
        }    
    }


    public float x = 10;
    public float y = 15+15;
    public float w = 100;
    public float h = 25;

    int joyIndex = 0;
    void Window_Console(int windowID)
    {
        GUILayout.BeginVertical();
        {
            GUILayout.BeginHorizontal();
            {
                GUILayout.FlexibleSpace();
                GUILayout.Label("Select Joystick");
                GUILayout.FlexibleSpace();
            }
            GUILayout.EndHorizontal();

            joyIndex = GUILayout.SelectionGrid(joyIndex, Input.GetJoystickNames(), 1);
            GUILayout.Space(10);

            GUILayout.BeginHorizontal();
            {
                GUILayout.FlexibleSpace();
                GUILayout.Label("Axis Values");
                GUILayout.FlexibleSpace();
            }
            GUILayout.EndHorizontal();

            AxisValues.Clear();
            for (int i = 0; i < 10; i++)
            {
                AxisValues.Add(string.Format("Axis {0} - Value: {1}", i, JoyAxis.getAxisValue(joyIndex, i).ToString("# 0.00")));
            }
            GUILayout.SelectionGrid(-1, AxisValues.ToArray(), 2, new GUILayoutOption[] { GUILayout.MinWidth(100) });
        }
        GUILayout.EndVertical();  
        GUI.DragWindow();
    }
    void Window_Joystick(int windowID)
    {
        GUILayout.BeginHorizontal();
        {
            GUILayout.BeginHorizontal();
            {
                showGUI_XBox = GUILayout.Toggle(showGUI_XBox, "XBox");
                showGUI_X56 = GUILayout.Toggle(showGUI_X56, "X56");
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginArea(new Rect(10, 40,336, 40), "Selet Joystick");
            GUILayout.Space(18);
            //JoyAxis.JoyIndex = GUILayout.Toolbar(JoyAxis.JoyIndex, Input.GetJoystickNames());
            //joyIndex = GUILayout.Toolbar(joyIndex, Input.GetJoystickNames());
            joyIndex = GUILayout.SelectionGrid(joyIndex, Input.GetJoystickNames(), 2);

            GUILayout.EndArea();

            GUILayout.BeginArea(new Rect(10, 68, 340, 120));
            GUILayout.Space(18);
            GUILayout.BeginVertical();

            GUILayout.BeginHorizontal();
            {
                GUILayout.Label(string.Format("Axis 0 Value: {0}", JoyAxis.getAxisValue(joyIndex,0).ToString("0.00")));
                GUILayout.Label(string.Format("Axis 1 Value: {0}", JoyAxis.getAxisValue(joyIndex, 1).ToString("0.00")));
                GUILayout.Label(string.Format("Axis 2 Value: {0}", JoyAxis.getAxisValue(joyIndex, 2).ToString("0.00")));
                GUILayout.Label(string.Format("Axis 3 Value: {0}", JoyAxis.getAxisValue(joyIndex, 3).ToString("0.00")));
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label(string.Format("Axis 4 Value: {0}", JoyAxis.getAxisValue(joyIndex, 4).ToString("0.00")));
                GUILayout.Label(string.Format("Axis 5 Value: {0}", JoyAxis.getAxisValue(joyIndex, 5).ToString("0.00")));
                GUILayout.Label(string.Format("Axis 6 Value: {0}", JoyAxis.getAxisValue(joyIndex, 6).ToString("0.00")));
                GUILayout.Label(string.Format("Axis 7 Value: {0}", JoyAxis.getAxisValue(joyIndex, 7).ToString("0.00")));
            }
            GUILayout.EndHorizontal();

            GUILayout.EndVertical();
            GUILayout.EndArea();
        }
        GUILayout.EndHorizontal();

        GUI.DragWindow();
    }
    //public static int select = 0;

    

    void Window_Axis(int windowID)
    {
        Rect crossRect = new Rect((windowRect_Axis.width - windowRect_Axis.width * 0.9f) * 0.5f, 46f * 0f+23, windowRect_Axis.width * 0.9f, windowRect_Axis.width * 0.9f);
        _graphRect = crossRect;

        //GUILayout.Box("Control Curve", new GUILayoutOption[] { GUILayout.Height(windowRect_Axis.width * 0.9f + 35) });
        DrawCrossRect(crossRect, Color.gray);
        DrawGraph();
        

        FillRect(new Rect(crossRect.x + crossRect.width * 0.5f + /*JoyAxis.getAxisValue(test_index, test_axisIndex)*/_joyAxis.Value * crossRect.width*0.5f, crossRect.y, 1, crossRect.height), Color.yellow);



        _joyAxis.JoyIndex /*= test_index*/ = AddMenu("Joystick", Input.GetJoystickNames(), /*test_index*/_joyAxis.JoyIndex);
        _joyAxis.AxisIndex /*= test_axisIndex*/ = AddMenu("Axis", new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }, /*test_axisIndex*/_joyAxis.AxisIndex);

        _joyAxis.Sensitivity = AddSlider("Sensitivity", _joyAxis.Sensitivity, 0f, 5f);
        _joyAxis.Curvature = AddSlider("Curvature", _joyAxis.Curvature, 0f, 3f);
        _joyAxis.Deadzone = AddSlider("Deadzone", _joyAxis.Deadzone, 0f, 0.5f);
        //foreach (var td in controls)
        //{
        //    td.value = AddToggle(td.title, td.value);
        //}

        GUI.DragWindow();
    }

    void Window_XBox(int windowID)
    {    
        GUILayout.BeginHorizontal();
        {
            GUI.BeginGroup(new Rect(8, 24, 100, 40));
            GUI.Box(new Rect(0, 0, 100, 40), "LB");
            GUI.Toggle(new Rect(42, 18, 10, 10), Input.GetKey(KeyCode.Joystick1Button4), /*"LB"*/"");
            GUI.EndGroup();

            GUI.HorizontalSlider(new Rect(118, 38, 120, 0), JoyAxis.getAxisValue(joyIndex, (int)AxisIndex.MiddleAxis), 1f, -1f);

            GUI.BeginGroup(new Rect(8 + 240, 24, 100, 40));
            GUI.Box(new Rect(0, 0, 100, 40), "RB");
            GUI.Toggle(new Rect(42, 18, 10, 10), Input.GetKey(KeyCode.Joystick1Button5), /*"LB"*/"");
            GUI.EndGroup();
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        {
            GUILayout.BeginVertical();
            {
                GUI.BeginGroup(new Rect(8, 72, 100, 100));
                GUI.Box(new Rect(0, 0, 100, 100), "JoyStick");
                GUI.Button(new Rect((JoyAxis.getAxisValue(joyIndex, 0) + 1) * 40, (JoyAxis.getAxisValue(joyIndex, 1) + 1) * 40, 20, 20), "+");
                GUI.EndGroup();

                GUI.BeginGroup(new Rect(8, 180, 100, 100));
                GUI.Box(new Rect(0, 0, 100, 100), "Pad");
                GUI.Button(new Rect((JoyAxis.getAxisValue(joyIndex, 5) + 1) * 40, (1 - JoyAxis.getAxisValue(joyIndex, 6)) * 40, 20, 20), "+");
                GUI.EndGroup();
            }
            GUILayout.EndVertical();

            GUILayout.BeginVertical();
            {
                GUI.BeginGroup(new Rect(118, 32 + 100, 50, 40));
                GUI.Box(new Rect(0, 0, 50, 40), "Back");
                GUI.Toggle(new Rect(18, 18, 10, 10), Input.GetKey(KeyCode.Joystick1Button6), /*"LB"*/"");
                GUI.EndGroup();

                GUI.BeginGroup(new Rect(128 + 60, 32 + 100, 50, 40));
                GUI.Box(new Rect(0, 0, 50, 40), "Start");
                GUI.Toggle(new Rect(18, 18, 10, 10), Input.GetKey(KeyCode.Joystick1Button7), /*"LB"*/"");
                GUI.EndGroup();

                GUI.BeginGroup(new Rect(118, 180, 50, 40));
                GUI.Box(new Rect(0, 0, 50, 40), "8");
                GUI.Toggle(new Rect(18, 18, 10, 10), Input.GetKey(KeyCode.Joystick1Button8), /*"LB"*/"");
                GUI.EndGroup();

                GUI.BeginGroup(new Rect(128 + 60, 180, 50, 40));
                GUI.Box(new Rect(0, 0, 50, 40), "9");
                GUI.Toggle(new Rect(18, 18, 10, 10), Input.GetKey(KeyCode.Joystick1Button9), /*"LB"*/"");
                GUI.EndGroup();
            }
            GUILayout.EndVertical();

            GUILayout.BeginVertical();
            {
                GUI.BeginGroup(new Rect(248, 72, 100, 100));
                GUI.Box(new Rect(0, 0, 100, 100), "Buttons");
                GUI.Toggle(new Rect(42, 18, 10, 10), Input.GetKey(KeyCode.Joystick1Button3), /*"LB"*/"");
                GUI.Toggle(new Rect(16, 46, 10, 10), Input.GetKey(KeyCode.Joystick1Button2), /*"LB"*/"");
                GUI.Toggle(new Rect(16 + 54, 46, 10, 10), Input.GetKey(KeyCode.Joystick1Button1), /*"LB"*/"");
                GUI.Toggle(new Rect(42, 74, 10, 10), Input.GetKey(KeyCode.Joystick1Button0), /*"LB"*/"");
                GUI.EndGroup();

                GUI.BeginGroup(new Rect(248, 180, 100, 100));
                GUI.Box(new Rect(0, 0, 100, 100), "JoyStick");
                GUI.Button(new Rect((JoyAxis.getAxisValue(joyIndex, (int)AxisIndex.RightHorizontalAxis) + 1) * 40, (JoyAxis.getAxisValue(joyIndex, (int)AxisIndex.RightVerticlAxis) + 1) * 40, 20, 20), "+");
                GUI.EndGroup();
            }
            GUILayout.EndVertical();
        }
        GUILayout.EndHorizontal();

        //GUILayout.BeginHorizontal();
        //{
        //    GUILayout.BeginArea(new Rect(10, 288, 336, 40), "Selet Joystick");
        //    GUILayout.Space(18);
        //    JoyAxis.JoyIndex = GUILayout.Toolbar(JoyAxis.JoyIndex, Input.GetJoystickNames());
        //    GUILayout.EndArea();
          
        //    GUILayout.BeginArea(new Rect(10, 316, 340, 120));
        //    GUILayout.Space(18);
        //    GUILayout.BeginVertical();

        //    GUILayout.BeginHorizontal();
        //    {
        //        GUILayout.Label(string.Format("Axis 0 Value: {0}", JoyAxis.getAxisValue(0).ToString("0.00")));
        //        GUILayout.Label(string.Format("Axis 1 Value: {0}", JoyAxis.getAxisValue(1).ToString("0.00")));
        //        GUILayout.Label(string.Format("Axis 2 Value: {0}", JoyAxis.getAxisValue(2).ToString("0.00")));
        //        GUILayout.Label(string.Format("Axis 3 Value: {0}", JoyAxis.getAxisValue(3).ToString("0.00")));
        //    }
        //    GUILayout.EndHorizontal();
        //    GUILayout.Space(10);
        //    GUILayout.BeginHorizontal();
        //    {
        //        GUILayout.Label(string.Format("Axis 4 Value: {0}", JoyAxis.getAxisValue(4).ToString("0.00")));
        //        GUILayout.Label(string.Format("Axis 5 Value: {0}", JoyAxis.getAxisValue(5).ToString("0.00")));
        //        GUILayout.Label(string.Format("Axis 6 Value: {0}", JoyAxis.getAxisValue(6).ToString("0.00")));
        //        GUILayout.Label(string.Format("Axis 7 Value: {0}", JoyAxis.getAxisValue(7).ToString("0.00")));
        //    }
        //    GUILayout.EndHorizontal();

        //    GUILayout.EndVertical();
        //    GUILayout.EndArea();
        //}
        //GUILayout.EndHorizontal();

      

        GUI.DragWindow();
    }

    void Window_X56(int windowID)
    {
        GUILayout.BeginVertical();
        {
            GUI.BeginGroup(new Rect(8, 24, 100, 100));
            GUI.Box(new Rect(0, 0, 100, 100), "JoyStick");
            GUI.Button(new Rect((JoyAxis.getAxisValue(1,0) + 1) * 40, (JoyAxis.getAxisValue(1,1) + 1) * 40, 20, 20), "+");
            GUI.EndGroup();

            GUI.BeginGroup(new Rect(8, 24+108, 100, 100));
            GUI.Box(new Rect(0, 0, 100, 100), "JoyStick");
            GUI.Button(new Rect((JoyAxis.getAxisValue(1, 3) + 1) * 40, (JoyAxis.getAxisValue(1, 4) + 1) * 40, 20, 20), "+");
            GUI.EndGroup();
        }
        GUILayout.EndVertical();

        GUILayout.BeginVertical();
        {
            GUI.BeginGroup(new Rect(116, 24, 150, 232-24));
            GUI.Box(new Rect(0, 0, 150, 232-24), "Sliders");

            var offset = 32+5f;

            GUI.HorizontalSlider(new Rect(118f * 0f + 16f, 38f + offset * 0f, 120f, 0f), JoyAxis.getAxisValue(1, 2), 1f, -1f);
            GUI.HorizontalSlider(new Rect(118f * 0f + 16f, 38f + offset * 1f, 120f, 0f), JoyAxis.getAxisValue(1, 5), 1f, -1f);
            GUI.HorizontalSlider(new Rect(118f * 0f + 16f, 38f + offset * 2f, 120f, 0f), JoyAxis.getAxisValue(1, 6), 1f, -1f);
            GUI.HorizontalSlider(new Rect(118f * 0f + 16f, 38f + offset * 3f, 120f, 0f), JoyAxis.getAxisValue(1, 7), 1f, -1f);
            GUI.HorizontalSlider(new Rect(118f * 0f + 16f, 38f + offset * 4f, 120f, 0f), JoyAxis.getAxisValue(0, 4), 1f, -1f);
            GUI.EndGroup();

        }
        GUILayout.EndVertical();

        GUILayout.Space(20);

        GUILayout.BeginVertical();
        {
            var offset_x = 266f/*+20f*/;
            GUI.BeginGroup(new Rect(8 + offset_x, 24, 100, 100));
            GUI.Box(new Rect(0, 0, 100, 100), "JoyStick");
            GUI.Button(new Rect((JoyAxis.getAxisValue(0, 0) + 1) * 40, (JoyAxis.getAxisValue(0, 1) + 1) * 40, 20, 20), "+");
            GUI.EndGroup();
      
            GUI.BeginGroup(new Rect(8 +offset_x, 24 + 108, 100, 100));
            GUI.Box(new Rect(0, 0, 100, 100), "JoyStick");        
            GUI.Button(new Rect((JoyAxis.getAxisValue(0, 2) + 1) * 40, (JoyAxis.getAxisValue(0, 3) + 1) * 40, 20, 20), "+");
            GUI.EndGroup();

            GUI.BeginGroup(new Rect(8 +offset_x, 24 + 216, 100, 100));
            GUI.Box(new Rect(0, 0, 100, 100), "JoyStick");
            GUI.Button(new Rect((JoyAxis.getAxisValue(0, 5) + 1) * 40, (JoyAxis.getAxisValue(0, 6) + 1) * 40, 20, 20), "+");
            GUI.EndGroup();
        }
        GUILayout.EndVertical();

        GUI.DragWindow();
    }

    public bool test_bool = true;

    List<testData> controls = new List<testData>()
    {
        new testData(){  title = "test data 1", value = false},new testData(){ title = "test data 2", value = true }
    };

    public float test_value = 5f;

    public int test_index = 0;
    public int test_axisIndex = 0;
    void Window_test(int windowID)
    {
        //GUILayout.BeginHorizontal();

        //GUILayout.Label("?");
        //GUILayout.EndHorizontal();
        //test_bool = AddToggle("test window", test_bool);





        //DrawRect(new Rect(21, 46, width * 0.475f, width * 0.475f), Color.gray);
        //DrawRect(new Rect(21 + width * 0.475f, 46, width * 0.475f, width * 0.475f), Color.gray);
        //DrawRect(new Rect(21, 46 + width * 0.475f, width * 0.475f, width * 0.475f), Color.gray);
        //DrawRect(new Rect(21 + width * 0.475f, 46 + width * 0.475f, width * 0.475f, width * 0.475f), Color.gray);
        GUILayout.Box("Control Curve", new GUILayoutOption[] { GUILayout.Height(windowRect_test.width * 0.9f + 35) });
        DrawCrossRect(new Rect((windowRect_test.width - windowRect_test.width * 0.9f) * 0.5f, 46, windowRect_test.width * 0.9f, windowRect_test.width * 0.9f), Color.gray);

        //GUILayout.BeginArea(new Rect(10, 10, 50, 50));

        //GUILayout.Box(GUIContent.none, RectStyle, new GUILayoutOption[] { GUILayout.Height(1) });
        //GUILayout.BeginHorizontal();
        //GUILayout.Box(GUIContent.none, RectStyle, new GUILayoutOption[] { GUILayout.Width(1) ,GUILayout.Height(49)});
        //GUILayout.FlexibleSpace();
        //GUILayout.Box(GUIContent.none, RectStyle, new GUILayoutOption[] { GUILayout.Width(1), GUILayout.Height(49) });
        //GUILayout.EndHorizontal();
        //GUILayout.Box(GUIContent.none, RectStyle, new GUILayoutOption[] { GUILayout.Height(1) });
        //GUILayout.EndArea();

        test_index = AddMenu("Joystick", Input.GetJoystickNames(), test_index);
        test_axisIndex = AddMenu("Axis", new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }, test_axisIndex);

        AddToggle("test window2", true);

        test_value = AddSlider("test slider", test_value);
        //int index = UnityEngine.Random.Range(1, 5);
        //for (int i = 0; i < index; i++)
        //{
        //    AddToggle("test toggle" + i, true);
        //}
        foreach (var td in controls)
        {
            td.value = AddToggle(td.title, td.value);
        }


        GUI.DragWindow();
    }

  
    public bool IsBuilding()
    {
        List<string> scene = new List<string> { "INITIALISER", "TITLE SCREEN", "LevelSelect", "LevelSelect1", "LevelSelect2", "LevelSelect3" };

        if (SceneManager.GetActiveScene().isLoaded)
        {
            if (!scene.Exists(match => match == SceneManager.GetActiveScene().name))
            {
                return true;
            }
            return false;
        } 
        return false;
    }

    public static bool AddToggle(string title, bool value = false)
    {
        GUILayout.BeginHorizontal(new GUILayoutOption[] { GUILayout.Height(30) });
        {
            GUILayout.Label(title, new GUILayoutOption[] { GUILayout.MinWidth(75), GUILayout.ExpandWidth(false), GUILayout.Height(25f) });
            GUILayout.FlexibleSpace();
            value = GUILayout.Toggle(value, "", new GUILayoutOption[] { GUILayout.Width(50), GUILayout.ExpandWidth(false) });
        }
        GUILayout.EndHorizontal();
        return value;
    }
    public static float AddSlider(string title, float value,float min=0f,float max = 1f)
    {
        GUILayout.BeginHorizontal(new GUILayoutOption[] { GUILayout.Height(30) });
        {
            GUILayout.Label(title, new GUILayoutOption[] { GUILayout.MinWidth(75), GUILayout.ExpandWidth(false), GUILayout.Height(25f) });
            //GUILayout.FlexibleSpace();
            GUILayout.Space(10);
            GUILayout.BeginVertical();
            GUILayout.Space(10);
            value = GUILayout.HorizontalSlider(value, min, max, new GUILayoutOption[] { GUILayout.MinWidth(75), GUILayout.Height(15f) });
            GUILayout.EndVertical();
            //GUILayout.Space(width * 0.05f);
            //GUILayout.FlexibleSpace();
            GUILayout.Space(10);
            value = float.Parse(GUILayout.TextField(value.ToString("#0.000"), new GUILayoutOption[] { GUILayout.MinWidth(50), GUILayout.ExpandWidth(false), GUILayout.Height(25f) }));
        }
        GUILayout.EndHorizontal();
        return value;
    }
    public static int AddMenu(string title,string[] items, int value = 0)
    {
        GUILayout.BeginHorizontal(new GUILayoutOption[] { GUILayout.Height(30)});
        {
            GUILayout.Label(title, new GUILayoutOption[] { GUILayout.MaxWidth(75),GUILayout.MinWidth(75) });
            if (GUILayout.Button("<", new GUILayoutOption[] { GUILayout.MaxWidth(30), GUILayout.MinWidth(30) }))
            {
                if (--value <0)
                {
                    value = items.Length - 1;
                }
            }
            GUILayout.Box(items[value]);
            if (GUILayout.Button(">", new GUILayoutOption[] { GUILayout.MaxWidth(30), GUILayout.MinWidth(30) }))
            {
                if (++value > items.Length-1)
                {
                    value = 0;
                }
            }
        }
        GUILayout.EndHorizontal();
        return value;
    }


    private static readonly Texture2D RectTexture = new Texture2D(1, 1);
    private static readonly GUIStyle RectStyle = new GUIStyle();
    private static readonly Color CurrentColor = new Color();

    internal static void DrawCrossRect(Rect rect, Color color)
    {
        DrawRect(new Rect(rect.x, rect.y, rect.width * 0.5f+1, rect.height * 0.5f), color);
        DrawRect(new Rect(rect.x + rect.width * 0.5f-1, rect.y, rect.width * 0.5f+1, rect.height * 0.5f), color);
        DrawRect(new Rect(rect.x, rect.y + rect.height * 0.5f-1, rect.width * 0.5f+1, rect.height * 0.5f+1), color);
        DrawRect(new Rect(rect.x + rect.width * 0.5f-1, rect.y + rect.height * 0.5f-1, rect.width * 0.5f+1, rect.height * 0.5f+1), color);
    }
    internal static void DrawRect(Rect position, Color color)
    {
        FillRect(new Rect(
            position.x,
            position.y,
            position.width,
            1),
            color);

        FillRect(new Rect(
            position.x,
            position.y + position.height - 1,
            position.width,
            1),
            color);

        FillRect(new Rect(
            position.x,
            position.y,
            1,
            position.height),
            color);

        FillRect(new Rect(
            position.x + position.width - 1,
            position.y,
            1,
            position.height),
            color);
    }

    private void DrawGraph(/*string title,params GUILayoutOption[] options*/)
    {
        if (_graphTex == null || _graphRect != _lastGraphRect)
        {
            _graphTex = new Texture2D((int)_graphRect.width, (int)_graphRect.height);
            for (int i = 0; i < _graphTex.width; i++)
                for (int j = 0; j < _graphTex.height; j++)
                    _graphTex.SetPixel(i, j, Color.clear);
            _resetTex = _graphTex.GetPixels();
        }
        if (/*_axis*/_joyAxis.Changed || _graphRect != _lastGraphRect)
        {
            _graphTex.SetPixels(_resetTex);
            float step = 0.5f / _graphTex.width;
            for (float xValue = -1; xValue < 1; xValue += step)
            {
                float yValue = /*_axis*/_joyAxis.Process(xValue);
                if (yValue <= -1f || yValue >= 1f) continue;
                float xPixel = (xValue + 1) * _graphTex.width / 2;
                float yPixel = (yValue + 1) * _graphTex.height / 2;
                _graphTex.SetPixel(Mathf.RoundToInt(xPixel), Mathf.RoundToInt(yPixel), Color.white);
            }
            _graphTex.Apply();
        }
        _lastGraphRect = _graphRect;
        GUILayout.Box(_graphTex/*, options*/);
    }
    internal static void FillRect(Rect position, Color color)
    {
        if (color != CurrentColor)
        {
            RectTexture.SetPixel(0, 0, color);
            RectTexture.Apply();

            RectStyle.normal.background = RectTexture;
        }

        GUI.Box(position, GUIContent.none, RectStyle);
        //GUILayout.Box(GUIContent.none, RectStyle);
    }


    class testData
    {
        public string title;

        public bool value;
    }

}
